package com.yx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yx.dao.OwnerMapper;
import com.yx.dao.ParkingMapper;
import com.yx.model.Owner;
import com.yx.model.Parking;
import com.yx.service.IParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class ParkingServiceImpl extends ServiceImpl<ParkingMapper, Parking> implements IParkingService {

    @Autowired
    private ParkingMapper parkingDao;

    @Autowired
    private OwnerMapper ownerMapper;

    @Override
    public PageInfo<Parking> findParkAll(int page, int pageSize, String numbers) {
        PageHelper.startPage(page, pageSize);
        //查询的结果集
        List<Parking> list = parkingDao.queryParkAll(numbers);
        PageInfo<Parking> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public IPage<Parking> findListByPage(Integer page, Integer pageCount) {
        IPage<Parking> wherePage = new Page<>(page, pageCount);
        Parking where = new Parking();

        return baseMapper.selectPage(wherePage, Wrappers.query(where));
    }

    @Override
    public int add(Parking parking) {
        return baseMapper.insert(parking);
    }

    @Override
    public int delete(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int updateData(Parking parking) {
        return baseMapper.updateById(parking);
    }

    @Override
    public Parking findById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<Parking> queryParkingAll() {
        return null;
    }

    @Override
    public List<Parking> queryParkingByStatus() {
        return parkingDao.queryParkAllByStatus();
    }

    @Override
    public IPage<Parking> findParkAll(Page<Parking> parking, String numbers) {

        // 查询数据
        QueryWrapper<Parking> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(numbers)) {
            wrapper.eq("numbers", numbers);
        }
        Page<Parking> parkingPage = parkingDao.selectPage(parking, wrapper);

        // 封装数据
        List<Parking> records = parkingPage.getRecords();
        records = records.stream().map(parking1 -> {
            Integer ownerId = parking1.getOwnerId();
            Owner owner = ownerMapper.selectById(ownerId);
            parking1.setOwner(owner);
            return parking1;
        }).collect(Collectors.toList());

        // 重新封装数据
        parkingPage.setRecords(records);
        return parkingPage;
    }
}
